package xyz.cloud.gulimall.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xyz.cloud.gulimall.coupon.entity.CouponEntity;
import xyz.cloud.gulimall.coupon.service.CouponService;
import xyz.cloud.gulimall.coupon.common.utils.PageUtils;
import xyz.cloud.gulimall.coupon.common.utils.R;



/**
 * 优惠券信息
 *
 * @author 王关霖
 * @email zx1092771091zx@163.com
 * @date 2020-04-28 17:14:09
 */
@RestController
@RequestMapping("coupon/coupon")
@Slf4j
@RefreshScope
public class CouponController {
    @Autowired
    private CouponService couponService;


    @Value("${user.name}")
    private String name;
    @Value("${user.age}")
    private Integer age;
    @RequestMapping("test")
    public R test(){
    return R.ok().put("name",name).put("age",age);
    }

    /**
     * 远程调用dome
     * 方法查询商品优惠券
     * 返回假数据 测试使用此方法啊
     */
    @RequestMapping("/member/list")
    public R GitshopBcoypon(){
        CouponEntity couponEntity=new CouponEntity();
        couponEntity.setCouponName("魅族优惠券");
        List<CouponEntity> list = couponService.list();
        for (CouponEntity s:list
             ) {
            log.info("优惠券"+s.toString());
        }
        return R.ok().put("coupon" ,Arrays.asList(couponEntity));
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.getById(id);

        return R.ok().put("coupon", coupon);
    }
    /**
     * 保存
     * 测试
     */
//    @RequestMapping("/save")
//    public R save(){
//        CouponEntity couponEntity   =new CouponEntity();
//        couponEntity.setCouponName("魅族优惠券");
//        boolean save = couponService.save(couponEntity);
//        log.info("ok"+save);
//        return R.ok();
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
